<template>
    <div>
        <div class="login-box">
            <h1 style="text-align: center; margin-bottom: 30px">登陆</h1>
            <el-form :model="user" size="large" :rules="rules" ref="ruleFormRef">
                <el-form-item prop="id" class="user-box">
                    <el-input v-model="user.id" :prefix-icon="UserFilled"/>
                </el-form-item>
                <el-form-item prop="password" class="user-box">
                    <el-input v-model="user.password" :prefix-icon="Lock" show-password/>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" style="width: 45%" @click="login">登陆</el-button>
                    <el-button type="primary" style="width: 45%" @click="register">注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup>
    import { ref, reactive, getCurrentInstance } from 'vue'
    import {UserFilled, Lock} from '@element-plus/icons-vue'
    import {ElMessage} from "element-plus";
    import request from "../utils/request";
    const { proxy } = getCurrentInstance()


    const user = reactive({
    })

    const login = () => {
        proxy.$refs.ruleFormRef.validate((valid) => {
            if(valid){
                //往后台发请求
                request.post('/user/login',user).then(res => {
                    if(res.code === 200){
                        ElMessage({
                            type: "success",
                            message: "登陆成功"
                        })
                        sessionStorage.setItem('token',res.data.token)
                        console.log(res.data.token)
                        //成功就共享个人信息数据
                        request.get('/user/getInfo',{
                          headers:{
                            'token': res.data.token
                          }
                        }).then(res1 => {
                          sessionStorage.setItem("user", JSON.stringify(res1.data.user))
                        })
                        //创建连接
                        proxy.$router.push('/')
                    }else{
                        ElMessage({
                            type: "error",
                            message: res.msg
                        })
                    }
                })
            }else{
                ElMessage({
                    type: "error",
                    message: "用户名或密码错误"
                })
            }
        })
    }

    const register = () => {
        proxy.$router.push('/register')
    }

    const rules = reactive({
        id: [
            { required: true, message: '请输入用户名！', trigger: 'blur' }
        ],
        password: [
            { required: true, message: '请输入密码！', trigger: 'blur' }
        ],
    })
</script>

<style>
.login-box {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 350px;
  padding: 40px;
  transform: translate(-50%, -50%);
  background: rgba(231, 227, 227, 0.5);
  box-sizing: border-box;
  box-shadow: 0 15px 25px rgba(0, 0, 0, .6);
  border-radius: 10px;
}
.login-box h1 {
  margin: 0 0 30px;
  padding: 0;
  color: rgb(15, 15, 15);
  text-align: center;
}

.login-box .user-box {
  position: relative;
}

.login-box .user-box el-form-item {
  width: 100%;
  padding: 10px 0;
  font-size: 16px;
  color: #fff;
  margin-bottom: 30px;
  border: none;
  border-bottom: 1px solid #fff;
  outline: none;
  background: transparent;
}

.login-box .user-box label {
  position: absolute;
  top: 0;
  left: 0;
  padding: 10px 0;
  font-size: 16px;
  color: #fff;
  pointer-events: none;
  transition: .5s;
}


@keyframes btn-anim1 {
  0% {
    left: -100%;
  }
  50%, 100% {
    left: 100%;
  }
}

.login-box a span:nth-child(2) {
  top: -100%;
  right: 0;
  width: 2px;
  height: 100%;
  background: linear-gradient(180deg, transparent, #03e9f4);
  animation: btn-anim2 1s linear infinite;
  animation-delay: .25s
}

@keyframes btn-anim2 {
  0% {
    top: -100%;
  }
  50%, 100% {
    top: 100%;
  }
}

.login-box a span:nth-child(3) {
  bottom: 0;
  right: -100%;
  width: 100%;
  height: 2px;
  background: linear-gradient(270deg, transparent, #03e9f4);
  animation: btn-anim3 1s linear infinite;
  animation-delay: .5s
}

@keyframes btn-anim3 {
  0% {
    right: -100%;
  }
  50%, 100% {
    right: 100%;
  }
}

.login-box a span:nth-child(4) {
  bottom: -100%;
  left: 0;
  width: 2px;
  height: 100%;
  background: linear-gradient(360deg, transparent, #03e9f4);
  animation: btn-anim4 1s linear infinite;
  animation-delay: .75s
}

@keyframes btn-anim4 {
  0% {
    bottom: -100%;
  }
  50%, 100% {
    bottom: 100%;
  }
}
</style>